const userServices = require('../service/userServices')
const {
    success,
    fail
} = require('../utils/R')
// const jwt = require('jsonwebtoken');
// const {jwt_secret} = require('../utils/config')
function userController() {
    return {
        add: async function (ctx, next) {
            const requestBody = ctx.request.body
            try {
                const data = await userServices.add(requestBody)
                ctx.body = success(data, '保存成功')
            } catch (error) {
                console.log('error', error)
                ctx.body = fail(error)
            }
            next()
        },
        //单条件模糊查询
        findByName:async function(ctx,next){
            const requestBody = ctx.request.body
            try {
                const data = await userServices.findByName(requestBody)
                ctx.body = success(data, '查询成功')
            } catch (error) {
                console.log('error', error)
                ctx.body = fail(error)
            }
            next()
        },
        findByPage:async function(ctx,next){
            const requestBody = ctx.request.body
            /*获取header中的token保存的信息*/
            /*

            const header = ctx.request.headers
            let authorization = header.authorization.split(' ').pop()
            let userData = jwt.verify(authorization,jwt_secret)
             */
            try {
                const data = await userServices.findByPage(requestBody)
                ctx.body = success(data, '查询成功')
            } catch (error) {
                console.log('error', error)
                ctx.body = fail(error)
            }
            next()
        },
        findById: async function (ctx, next) {
            try {
                const {
                    id
                } = ctx.params
                const data = await userServices.findById(id)
                ctx.body = success(data, '查询成功')
            } catch (error) {
                ctx.body = fail(error)
            }
            next()
        },
        findAll: async function (ctx, next) {
            try {
                const data = await userServices.findAll()
                ctx.body = success(data, '查询成功')
            } catch (error) {
                ctx.body = fail(error)
            }
            next()
        },
        edit: async function (ctx, next) {
            try {
                const requestBody = ctx.request.body
                const data = await userServices.edit(requestBody)
                ctx.body = success(data, '修改成功')
            } catch (error) {
                ctx.body = fail(error)
            }
            next()
        },
        del: async function (ctx, next) {
            try {
                const {
                    id
                } = ctx.params
                const data = await userServices.del(id)
                ctx.body = success(data, '删除成功')
            } catch (error) {
                ctx.body = fail(error)
            }
            next()
        }
    }
}

module.exports = userController()